    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleshipgame.logic;
//import java.util.ArrayList;
//import java.util.HashMap;
import battleshipgame.logic.MarkMatrix;
import java.util.ArrayList;
import java.util.HashMap;
//import Excersize2_Swing.CommandSetnUnSetBattleship;
import Excersize2_Swing.SwingConstants;
import Excersize2_Swing.SwingConstants.BattleshipTypes;
import battleshipgame.manager.GameManager;

/**
 *
 * TODO: split player to two players: Computer and human
 */
public class Player {

//    public static enum BattleshipTypes {
//
//        AIRCRAFT_CARRIER, BATTLESHIP, SUBMARINE, CRUSIER, DESTROYER
//    }
    protected boolean connected;
    protected String nickname;
    protected int id; // can not change ID after initialization
    protected Board board; // the player's board
    protected HashMap<String, MarkMatrix> markBoards;
    protected Battleship[] battleshipList; // list of all the related battleships to player
    //protected ArrayList<CommandSetBattleship> CommandList ;
    public static String[] battleShipNamesList = {"aircraft carrier", "battleship", "submarine", "cruiser", "destroyer"};
    protected int remainingBattleshipCounter; // count how many battleship remained
    public ArrayList<BattleshipTypes> battleshipTypeses;
    private int totalBattleship = 0;
    TypeHumanOrBot humanOrBot ;
    private Player currentOpponent;
    private GameManager gameManager;

    public int getTotalBattleship() {
        return totalBattleship;
    }

    public boolean hasBattleship(String battleship) {
        boolean found = false;
        for(int i=0; i<battleshipList.length; i++)
        {
            if(battleshipList[i] != null && battleshipList[i].getName().equals(battleship))
            {
                found = true;
            }

        }
        return found;
    }

    public void withdraw() {
        remainingBattleshipCounter = 0;
        gameManager.advanceToNextPlayer();
    }

    public static enum TypeHumanOrBot {

        HUMAN, BOT
    };

    public static enum Direction {

        HORIZONTAL, VERTICAL, NONE
    };//eliran

//@eliran: Constractor
     public Player(String nickname, int id, TypeHumanOrBot type, GameManager gameManager) {
         this(nickname, id, gameManager);
         humanOrBot = type;
    }

    public Player(String nickname, int id, GameManager gameManager) {
        this.nickname = nickname;
        this.id = id;
        board = new Board();
        remainingBattleshipCounter = Constants.NUM_BATTLESHIPS;
        markBoards = new HashMap<String, MarkMatrix>();
        battleshipTypeses = new ArrayList<BattleshipTypes>();
        humanOrBot = TypeHumanOrBot.HUMAN;
        connected = false;
        this.gameManager = gameManager;
        initBattleshipList(5);
    }

    public void setId(int id) {
        this.id = id;
    }

    public void initMarkBoards(int numOfPlayers, ArrayList<Player> players) {
        for (int i = 0; i < numOfPlayers; i++) {
            markBoards.put(players.get(i).getName(), new MarkMatrix());
        }
    }

    public MarkMatrix getMarkBoard(String playerName) {
        return markBoards.get(playerName);
    }

    /*
     *@shaked marks a player
     */
    public void markPlayer(Player opponent, String coordiante) throws InvalidInputException {
        MarkMatrix temp;
        temp = markBoards.get(opponent.getName());
        temp.Mark(coordiante);
    }

    //@shaked
    public void initBattleshipList(int numOfBattleships) {
        battleshipList = new Battleship[numOfBattleships];
    }

    public boolean bombPlayer(Player AttackedPlayer, String coordinate) throws InvalidInputException, AlreadyBombedException {
        //@eliran : boolean so we return false when the square is already bombed or somthing...
        boolean flag = AttackedPlayer.board.bombByCoordinate(coordinate, AttackedPlayer);
        return flag;
    }

    public void BattleshipWasDestroyed() { // update RemainingShips
        remainingBattleshipCounter--;
    }

    public int getRemainingBattleships()
    {
        return remainingBattleshipCounter;
    }
    public void SetBattleshipInBoard(Battleship battleship, String coordinate, Direction direction) throws InvalidInputException {
        board.InsertBattleship(battleship, coordinate, direction);
        battleship.setCoordinate(coordinate);
        battleship.setDirection(direction);
        battleshipList[totalBattleship++] = battleship;
        addBattleshipType(SwingConstants.getBattlehipTypeByString(battleship.getName()));

    }

    public void RemovBattleshipFromBoard (Battleship battleship) throws InvalidInputException {
        Battleship [] tempbattleshipList ;
        int i , k ;
        tempbattleshipList = new Battleship[5];
        for (  i= 0 ,k =0  ; i < totalBattleship ; i++)
            if ( !battleshipList[i].getCoordinate().equals(battleship.getCoordinate()))  
                tempbattleshipList [k++] = battleshipList[i] ;
        board = new Board();
        String coordinate;
        Direction direction ;
        totalBattleship = 0;
        battleshipTypeses.removeAll(battleshipTypeses);
        initBattleshipList(5);
        for ( i=0 ; i < k ; i++ ) {
            coordinate = tempbattleshipList[i].getCoordinate();
            direction = tempbattleshipList[i].getDirection();
            SetBattleshipInBoard(tempbattleshipList[i], coordinate, direction);
        }   
    }
//another implementation of c'tor:

//    public void SetBattleshipInBoard(Battleship battleship,
//            String coordinate, Direction direction, BattleshipTypes battleshipTypes) throws InvalidInputException {
//        board.InsertBattleship(battleship, coordinate, direction);
//        battleship.setCoordinate(coordinate);
//        battleship.setDirection(direction);
//        battleshipList[totalBattleship++] = battleship;
//        addBattleshipType(battleshipTypes);
//    }

    public void addBattleshipType(BattleshipTypes type) {
        battleshipTypeses.add(type);
    }

    public Battleship getBattleShip(int i )
    {
        return battleshipList[i];
    }
    public ArrayList<BattleshipTypes> getBattleshipTypeses() {
        return battleshipTypeses;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public boolean IsPlayerDead() {
        if (remainingBattleshipCounter == 0) {
            return true;
        } else {
            return false;
        }
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return nickname;
    }

    public Board getBoard() {
        return board;
    }

    public TypeHumanOrBot getHumanOrBot() {
        return humanOrBot;
    }

    public void setHumanOrBot(TypeHumanOrBot humanOrBot) {
        this.humanOrBot = humanOrBot;
    }

    public void connect()
    {
        connected = true;
    }

    public boolean isConnected()
    {
        return connected;
    }

    public Player getCurrentOpponent()
    {
        return currentOpponent;
    }
    public void setCurrentOpponent(Player currentOpponent)
    {
        this.currentOpponent = currentOpponent;
    }
    
}